package cn.yanglq.Rss.web.util;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 短信验证码生成工具
 *
 * @author Administrator
 */
public final class SmsIdentifyingCode {
    private SmsIdentifyingCode() {
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    private static String[] baseCode = new String[]{"0", "1", "2", "3", "4",
            "5", "6", "7", "8", "9"};

    /**
     * 获取生成的验证码
     *
     * @param length 设置要生成的验证码长度
     * @return
     */
    public static String create(int length) {

        List<String> list = Arrays.asList(baseCode);
        Collections.shuffle(list);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i));
        }
        String shuffleCode = sb.toString();
        String result = shuffleCode.substring(0, length);

        return result;
    }

    public static void main(String[] args) {
        System.out.println(SmsIdentifyingCode.create(4));
    }

}
